package co.com.jaco.bl.aas.admin;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import co.com.jaco.dto.aas.PersonDTO;
import co.com.jaco.dto.aas.RolDTO;
import co.com.jaco.entity.aas.Privilege;
import co.com.jaco.entity.aas.PrivilegePK;
import co.com.jaco.entity.aas.Rol;
@Stateless 
public class RoleManagementBean implements RoleManagementLocal {
	@PersistenceContext 
	EntityManager persistence;
	
	@Override
	public void createRole(RolDTO rol) {
		persistence.persist(rol);
	}

	@Override
	public void assignRole(RolDTO rol, PersonDTO person) {
		Privilege privilege = new Privilege();
		PrivilegePK privilegePK = new PrivilegePK();
		privilegePK.setRolePkCode(rol.getPkCode());
		privilegePK.setUserPkCode(person.getPkCode());
		privilege.setId(privilegePK);
		persistence.persist(privilege);
	}

	@Override
	public List<RolDTO> listRoles() {
		TypedQuery<Rol> tQuery = persistence.createNamedQuery("roles.listAllRoles", Rol.class);
		List<Rol> roles = tQuery.getResultList();
		List<RolDTO> rolesDTO = new ArrayList<RolDTO>();
		for (Rol rol : roles )
		{
			RolDTO rolDTO = new RolDTO();
			rolDTO.setDescription(rol.getDescription());
			rolDTO.setName(rol.getName());
			rolDTO.setPkCode(rol.getPkCode());
		}
		return rolesDTO;
	}

	@Override
	public RolDTO findRole(RolDTO filter) {
		TypedQuery<Rol> tQuery = persistence.createNamedQuery("roles.findRoleById", Rol.class);
		tQuery.setParameter("code_pk", filter.getPkCode());
		Rol rol = tQuery.getSingleResult();
		RolDTO rolDTO = new RolDTO();
		rolDTO.setDescription(rol.getDescription());
		rolDTO.setName(rol.getName());
		rolDTO.setPkCode(rol.getPkCode());
		return rolDTO;
	}

	@Override
	public void editRole(RolDTO role) {
		throw new NotImplementedException();
	}

	@Override
	public List<RolDTO> listRoles(RolDTO filter) {
		throw new NotImplementedException();
	}
	

}
